<template>
	<view class="chat">
		<view class="chat-bg"></view>
		<scroll-view class="scroll" scroll-y show-scrollbar :scroll-into-view="toButtom" @scroll="showScroll">
			<view class="chat-info" :class="item.sendUser === 0 ? 'chat-self' : 'chat-chatter'"
				v-for="(item, index) in chatList" :key="index">
				<image :src="item.sendUser === 0 ? user.cover :  other.cover" mode="aspectFill"
					style="height: 120rpx; width: 120rpx; border-radius: 50%; margin-right: 15rpx; margin-bottom: auto;"
					@click="toInfo(item.sendUser,other.id)">
				</image>
				<view class="chat-message">
					<text class="text-main-xll">{{ item.sendMessage }}</text>
				</view>
			</view>
			<view id="to-buttom" style="height: 12rpx;"></view>
		</scroll-view>
		<!-- 		<view class="zhanweifu"></view> -->

		<view class="input-box">
			<view class="tools">
				<view class="clear" @click="clear">
					<uni-icons type="trash" size="24"></uni-icons>
				</view>
			</view>
			<input type="text" v-model="other.sendMessage">
			<button @click="sendMessage">发送消息</button>
		</view>
	</view>
</template>

<script>
	import {
		getDetail
	} from '@/common/mock/mock.js';
	import {
		getTime
	} from '@/utils/getTime.js'
	import listItem from '../../uni_modules/uview-ui/libs/config/props/listItem';
	export default {
		data() {
			return {
				chatList: [],
				user: {
					id: '',
					cover: ''
				},
				other: {
					id: '',
					username: '',
					cover: '',
					lastMessage: '',
					lastTime: '',
					sendMessage: ''
				},
				toButtom: '',
				socketMessage: '',
			}
		},
		onLoad(option) {
			this.openSocket();
			if (option.id) {
				this.getData(option.id);
				// 
				this.other.id = option.id;
			};
			uni.setNavigationBarTitle({
				title: option.username
			});
			uni.setNavigationBarColor({
				frontColor: "#000000",
				backgroundColor: '#00b4b3'
			});
			this.other.cover = option.cover;
			this.user.cover = this.$store.state.user.cover;
			this.InsertChatList(uni.getStorageSync(option.id))
		},
		onUnload() {
			uni.setStorageSync(this.other.id, this.chatList);

			this.$store.state.noneMessage = false;
			console.log(this.$store.state.noneMessage)

			uni.closeSocket({})
			console.log("关闭了当前对话...")
		},
		methods: {
			getData(id) {
				// mock
				// getDetail(id).then((res) => {
				// 	this.boss.id = res._id.$oid,
				// 		this.boss.job = res.job,
				// 		this.boss.price = res.price,
				// 		this.boss.address = res.address,
				// 		this.boss.cover = res.cover,
				// 		this.boss.interviewer = res.interviewer,
				// 		// this.context = res.context,
				// 		this.boss.label = res.label,
				// 		this.boss.company = res.company,
				// 		this.chatData.bossImage = res.cover
				// })
			},
			openSocket() {
				uni.connectSocket({
					url: 'ws://localhost:3200'
				});
				uni.onSocketError(function(res) {
					console.log('WebSocket连接打开失败，请检查！');
				});

				uni.onSocketOpen((res) => {
					console.log('WebSocket连接已打开！');
				});
			},
			sendMessage() {
				if (this.other.sendMessage) {
					// 我方接受数据
					// 还需设计
					let message = {
						to: this.$store.state.user._id,
						text: this.other.sendMessage
					};
					console.log(message)

					uni.sendSocketMessage({
						data: JSON.stringify(message)
						// data: JSON.stringify(this.chatData.sendMessage),
					}).then(() => {
						console.log("发送了" + JSON.stringify(message))
						// console.log("发送了" + this.chatData.sendMessage)
						let chat_self = {
							sendUser: 0,
							sendMessage: this.other.sendMessage
						}
						this.chatList.push(chat_self);
						// 存储最后一条数据
						this.other.lastMessage = this.other.sendMessage;
						console.log("lastMessage")
						console.log(this.other.lastMessage)
						this.other.lastTime = getTime();

						this.other.sendMessage = '';

						// 对方发送数据
						uni.onSocketMessage((res) => {
							let data = JSON.parse(res.data)
							if (data.to != this.$store.state.user._id) {
								console.log("data.to :" + data.to);
								console.log("this.$store.state.user._id:" + this.$store.state.user._id)
								console.log("data.to != this.$store.state.user._id:" + data.to != this
									.$store.state.user._id)
								var chatter = data.text;
								console.log(chatter);
								let chat_chatter = {
									sendUser: 1,
									sendMessage: chatter
								}
								this.chatList.push(chat_chatter);

								this.other.lastMessage = chatter;
								this.other.lastTime = getTime();
							}



						})
						console.log(this.other)
					}).catch(() => {
						uni.showToast({
							title: '通信开了会差┭┮﹏┭┮ 重新连接中',
							icon: 'none'
						});
						reconnect();
					})
				} else {
					uni.showToast({
						title: '请在输入框内填写内容...',
						icon: 'none'
					});
				}
				console.log(this.other)
				this.toBottom();
				// console.log(this.toButtom);
			},
			showScroll() {
				this.toButtom = '',
					console.log("你有滚了")
			},
			toBottom() {
				this.toButtom = 'to-buttom';
			},
			InsertChatList(List) {
				for (let i = 0; i < List.length; i++) {
					this.chatList.push(List[i])
				}
			},
			clear() {
				this.chatList = []
				uni.showToast({
					title: '聊天记录已删除',
					icon: 'none'
				});
			},
			toInfo(id, user_id) {
				console.log("id")
				console.log(id)
				console.log('user_id')
				console.log(user_id)
				this.$u.route('pages/myInfo/myInfo', {
					id: id,
					user_id: user_id
				})
			}
		}
	}
</script>

<style lang="scss">
	page {
		// background: red;
	}

	.chat-bg {
		height: 100vh;
		width: 100vw;
		background-color: gainsboro;
		// background-image: linear-gradient(to top, gainsboro 0%, #ace0f9 100%);
		background-size: cover;
		box-sizing: border-box;
		position: fixed;
		z-index: 0;
	}


	.chat {
		background: gainsboro;
		width: 100%;

		.scroll {
			height: 82vh;

			.chat-info {
				display: flex;
				align-items: center;
				margin-bottom: 12rpx;

				.chat-message {

					max-width: 80%;
					padding: 15rpx;
					font-weight: bold;
					border-radius: 15rpx;
					box-shadow: rgba(60, 64, 67, 0.3) 0rpx 1rpx 2rpx 0rpx, rgba(60, 64, 67, 0.15) 0rpx 1rpx 3rpx 1rpx;
				}
			}

			.chat-chatter {
				margin-left: 15rpx;

				.chat-message {
					background-image: linear-gradient(-225deg, #fffeff 0%, #d7fffe 100%);
				}
			}

			.chat-self {
				flex-direction: row-reverse;
				margin-left: auto;

				.chat-message {
					margin: 10rpx;
					background-image: linear-gradient(-20deg, #e9defa 0%, #fbfcdb 100%);
				}

			}


		}

		.zhanweifu {
			height: 175rpx;
		}



		.input-box {
			position: fixed;
			z-index: 99;
			bottom: 0;
			width: 100%;
			height: 8vh;
			padding: 10px;
			display: flex;
			flex-direction: row;
			align-items: center;
			background: white;

			.tools {
				background: red;
				height: 6vh;
				z-index: 99;
				width: 10vw;
				border-radius: 30%;
				display: flex;
				align-items: center;
				justify-content: center;
			}

			input {
				float: left;
				width: 58vw;
				border: 1px solid black;
				border-radius: 20rpx;
				height: 50%;
				margin-left: 1%;
				padding: 5px 5px 5px 15px;
				background: white;
			}

			button {
				position: absolute;
				float: left;
				left: 76%;
				width: 20vw;
				height: 50%;
				margin-right: 10rpx;
				border-radius: 20rpx;
				background: $boss-base-color;
				text-align: center;
				// align-items: center;
			}
		}
	}
</style>